home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / mcu / hc05iic.arc / IIC.S < prev   
Text File  |  1990-06-15  |  4KB  |  107 lines

  1.         opt     mul,cmos,lle=132
  2. * Software for 68hc05 to control IIC peripherals using the SPI interface
  3. * adapted from Naji Naufel's article in EDN, Feb 18 1988
  4. * Port C Bits 0 and 1 are used to generate the start and stop bits required
  5.  
  6.         opt     nol
  7.         include c4c8reg.s
  8.         opt     l
  9.  
  10.         asct
  11. data    equ     PC0             IIC data line controlled by PC0
  12. .data   equ     .PC0
  13. clk     equ     PC1             IIC clock line controlled by PC1
  14. .clk    equ     .PC1
  15. iicport equ     PORTC
  16. iicddr  equ     DDRC
  17.  
  18. waddr   equ     0               IIC write address
  19. raddr   equ     1               IIC read address
  20.  
  21. * init initializes the I/O port hardware
  22.  
  23.         org     $50             RAM location - ACK flag
  24. ack     rmb     1               set bit 0 to value of ACK bit desired after byte
  25. control rmb     1               RAM location to store control word to IIC device
  26. ram     rmb     4               ram buffer for read command
  27.  
  28.         org     $100            start of ROM
  29. init    lda     #.data+.clk     set clock and data high
  30.         sta     iicport
  31.         sta     iicddr          both bits output
  32.         clr     SPCR            make sure SPI is didsabled to start
  33.         rts                     done
  34.  
  35. * w_start transfers a byte from the HC05 SPI to the IIC peripheral
  36. * Including a start bit.  nostart sends the byte with no start bit
  37. * On entry the data is in the accumulator
  38.  
  39. w_start bclr    data,iicport    set data line low - start bit
  40.         bclr    clk,iicport     drive clock low
  41. nostart ldx     #.SPE+.MSTR     enable spi
  42.         stx     SPCR
  43.         bset    data,iicport    let data line go high
  44.         sta     SPDR            send the data
  45. wait    brclr   SPIF,SPSR,wait  poll while data shifted out
  46. *                               set SPIE bit for interrupt-driven transfer
  47.         lda     SPCR            disable SPI
  48.         and     #.SPE!X$ff
  49.         sta     SPCR
  50.         brset   0,ack,hiack     test value of ack bit desired
  51.         bclr    data,iicport    ack must be low - clear data line
  52.         bsr     hiack           generate clock pulse
  53.         bset    data,iicport    let data go high again
  54.         rts
  55.  
  56. hiack   bset    clk,iicport     pulse clock high, then low
  57.         brn     hiack           delay - 3 'E' clocks
  58.         bclr    clk,iicport
  59.         rts
  60.  
  61. * This subroutine creates a stop condition
  62.  
  63. stop    bclr    data,iicport    strobe data low
  64.         bset    clk,iicport     positive edge on clock line
  65.         bset    data,iicport    put data back to idle state
  66.         rts
  67.  
  68. * This subroutine sends an address byte, followed by a control byte in CONTROL
  69.  
  70. addrcnt lda     #waddr          load address value
  71.         bsr     w_start         send with start bit
  72.         lda     control         get desired control value
  73.         bsr     nostart         send without start bit
  74.         rts                     done
  75.  
  76. * This subroutine reads four bytes from the peripheral
  77.  
  78. read    clr     control         control = 0
  79.         bset    0,ack           ack bit should be 1
  80.         bsr     addrcnt         send address followed by control
  81.         bsr     stop            send stop bit
  82.         lda     #raddr          now send read address
  83.         bsr     w_start
  84.         bclr    0,ack           ack bit is 1
  85.         lda     #$ff            read 4 bytes - must send ones during process
  86.         bsr     nostart
  87.         lda     SPDR            get received data
  88.         sta     ram+2           save in RAM
  89.         lda     #$ff            read 4 bytes - must send ones during process
  90.         bsr     nostart
  91.         lda     SPDR            get received data
  92.         sta     ram+3           save in RAM
  93.         lda     #$ff            read 4 bytes - must send ones during process
  94.         bsr     nostart
  95.         lda     SPDR            get received data
  96.         sta     ram+1           save in RAM
  97.         bset    0,ack           last byte has ack bit set to 1
  98.         lda     #$ff            read 4 bytes - must send ones during process
  99.         bsr     nostart
  100.         bsr     stop            send stop bit
  101.         lda     SPDR            get received data
  102.         sta     ram             save in RAM
  103.         rts                     done
  104.  
  105.         end
  106.  
  107.